package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.util.MyPage;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.AdvposDto;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.util.MyPage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.service.YddAdvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @GetMapping("/list")
    public R<MyPage> list(String advposdesc, String advposname,
                          @RequestParam(defaultValue = "1") String page, @RequestParam(value = "limit", defaultValue = "10") String size) {
        Page<YddAdvpos> advposPage = new Page(Integer.valueOf(page), Integer.valueOf(size));
        MyPage<AdvposDto> advposMyPage = new MyPage<>();
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(YddAdvpos::getId);
        queryWrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        yddAdvposService.page(advposPage, queryWrapper);

        //封装进返回对象的数据
        List<YddAdvpos> records = advposPage.getRecords();
        List<AdvposDto> data = records.stream().map((item) -> {
            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(item, advposDto);
            QueryWrapper<YddAdvimg> wrapper = new QueryWrapper<>();
            wrapper.eq("advimgpos", item.getId());
            int num = yddAdvimgService.count(wrapper);
            advposDto.setAdvposnum(num);
            return advposDto;
        }).collect(Collectors.toList());
        advposMyPage.setData(data);
        advposMyPage.setPageSize(Long.valueOf(size));
        advposMyPage.setCurrentPage(Long.valueOf(page));
        advposMyPage.setTotalPages(advposPage.getPages());
        advposMyPage.setCount(advposPage.getTotal());
        return R.success(advposMyPage);
    }
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.save(yddAdvpos);
        return R.success("添加成功");
    }
    @GetMapping("/del")
    public R<String> delete(String id){
        yddAdvposService.removeById(id);
        return R.success("删除成功");
    }
@GetMapping("rowInfo")
    public R<YddAdvpos> rowInfo(String id){
    YddAdvpos yddAdvpos = yddAdvposService.getById(id);
    return R.success(yddAdvpos);
}
@PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.updateById(yddAdvpos);
        return R.success("修改成功");
}



	@GetMapping("/all")
	public R<List<YddAdvpos>> all(){
		List<YddAdvpos> advposList = yddAdvposService.list();
		return R.success(advposList);
	}
}
